﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SKN.TestDataTool
{
    public partial class frmTestData : Form
    {
        public frmTestData ()
        {
            InitializeComponent();
        } 

        /// <summary>
        /// This event will load the lists for the given web url
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>      
        private void btnLoadLists_Click (object sender, EventArgs e)
        {
            string siteUrl = txtSiteUrl.Text.Trim();
            List<string> allLists = new List<string>();

            using (SPSite site = new SPSite(siteUrl))
            {
                txtMessage.Text += "Site Collection Found";
                using (SPWeb web = site.OpenWeb())
                {
                    txtMessage.Text += Environment.NewLine + "Site Found";

                    foreach (SPList spList in web.Lists)
                    {
                        if (spList.BaseType == SPBaseType.GenericList
                            || spList.BaseType == SPBaseType.DiscussionBoard)
                        {
                            allLists.Add(spList.Title);
                        }
                    }
                }
            }

            cmbLists.DataSource = allLists;
            cmbLists.SelectedIndex = -1;

            txtMessage.Text += Environment.NewLine + "All lists loaded for selection.";

        }  

        /// <summary>
        /// Start the data population process
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click (object sender, EventArgs e)
        {
            bool _error = false;
            SharePointUtility sharepointUtility = new SharePointUtility();

            try
            {
                string siteUrl = txtSiteUrl.Text.Trim();
                string listName = cmbLists.SelectedValue.ToString();
                int itemCount = int.Parse(txtItemsCount.Text.Trim());
                int subItemsCount = int.Parse(txtMaxSubItemsCount.Text.Trim());
                bool cleanListFirst = chkCleanList.Checked;

                // validate all input fields
                if (!(siteUrl.Length > 0
                    && listName.Length > 0
                    && itemCount > 0
                    && subItemsCount > 0))
                {
                    _error = true;
                    txtMessage.Text = "Please enter valid inputs.";
                }

                if (!_error)
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        txtMessage.Text = "Site Collection Found";
                        using (SPWeb web = site.OpenWeb())
                        {
                            txtMessage.Text += Environment.NewLine + "Site Found";
                            SPList list = web.Lists[listName];

                            txtMessage.Text += Environment.NewLine + "Starting process...";

                            if (null != list)
                            {
                                switch (list.BaseType)
                                {
                                    case SPBaseType.DiscussionBoard:
                                        txtMessage.Text += "List Type : Discussion";
                                        sharepointUtility.LoadDiscussionList(list, itemCount, subItemsCount, cleanListFirst);
                                        txtMessage.Text += sharepointUtility._message;
                                        break;

                                    case SPBaseType.GenericList:
                                        txtMessage.Text += "List Type : Generic";
                                        sharepointUtility.LoadGenericList(list, itemCount, cleanListFirst);
                                        txtMessage.Text += sharepointUtility._message;
                                        break;

                                    default:
                                        txtMessage.Text += "List Type : No Support by tool.";
                                        break;
                                }
                            }

                            txtMessage.Text += Environment.NewLine + "Done.";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                txtMessage.Text += Environment.NewLine + "=========================";
                txtMessage.Text += Environment.NewLine + ex.ToString();
                txtMessage.Text += Environment.NewLine + "=========================";
            }
        }      
    }
}
